package mailSendService.service;

import com.itextpdf.html2pdf.ConverterProperties;
import com.itextpdf.html2pdf.HtmlConverter;
import com.itextpdf.html2pdf.resolver.font.DefaultFontProvider;
import mailSendService.module.PdfSignUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.FileSystemResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;


@Service
public class MailContextEditService {

    private static Logger logger = LoggerFactory.getLogger(MailSenderService.class);
    private Resource fileSystemResource = new FileSystemResourceLoader().getResource("classpath:/config/fonts"); //default font resources

    public boolean convertHtmlToPdf(String inputFile, String outputFile){

        try {
            File file = new File(inputFile);
            if (file.exists()) return false;
            FileInputStream inFile = new FileInputStream(inputFile);
            FileChannel fileChannel = inFile.getChannel();
            ByteBuffer byteBuffer = ByteBuffer.allocate((int) file.length());
            fileChannel.read(byteBuffer);
            String htmlContext = new String(byteBuffer.array());
            generate(htmlContext, outputFile);
        }catch (IOException e){
            logger.error("文件没有被找到",e);
        }
        return true;
    }

    //输出到pdf
    public boolean generate(String htmlContext, String outputFile){
        boolean inputS = false;
        ConverterProperties props = new ConverterProperties();
        DefaultFontProvider defaultFontProvider = new DefaultFontProvider();
        props.setFontProvider(defaultFontProvider);
        File file = new File(outputFile);
        if (!file.getParentFile().exists()) file.getParentFile().mkdirs();
        if (file.exists()) return false; // if exist return
        try {
            if (fileSystemResource.exists()) { //如果存在
                defaultFontProvider.addDirectory(fileSystemResource.getURL().getPath()); //添加自定义字体
            }
            HtmlConverter.convertToPdf(htmlContext, new FileOutputStream(outputFile), props);
            inputS = true;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return inputS;
    }

    public void setFontResource(String resourcePath){
        this.fileSystemResource = new FileSystemResourceLoader().getResource(resourcePath);
    }

    //pdf 进行签章
    public void sign(String inputPath, String outputPath){
        String moulagePath = "D:\\MySource\\mailSend\\src\\main\\resources\\config\\signImage\\signature.png";
        String certPath = "D:\\MySource\\mailSend\\src\\main\\resources\\config\\keystore\\20060400000044502.p12";
        String password = "111111";
        PdfSignUtil.signPDF(inputPath, outputPath, moulagePath, certPath, password);
    }



}
